/** CS2340 Spring 2011 Project
 *  Team: 40 Jiggawatt Hard Driver
 *  Suk Hwan Hong
 *  Rhys Saraceni
 *  Travis Cooper
 *  Luke Stubb
 *
 * @author 40 Jiggawatt Hard Driver
 * @version 1.0 
 */

package edu.gatech.jiggawatt.office.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import edu.gatech.jiggawatt.office.model.Appointment;
import edu.gatech.jiggawatt.office.model.DailySchedule;
import edu.gatech.jiggawatt.office.model.Doctor;
import edu.gatech.jiggawatt.office.model.Patient;
import edu.gatech.jiggawatt.office.model.Time;
import edu.gatech.jiggawatt.office.presenter.Presenter;
import javax.swing.JTextField;

/**
 * Represents "View Edit Appointment Frame"
 * @author 40 Jiggawatt Hard Driver
 * @version 1.0
 *
 */
public class ViewEditAppointmentFrame extends JFrame { // $codepro.audit.disable declareDefaultConstructors

	private Appointment currentApp;
	private JPanel contentPane;
	private JTextField txtField_date;
	private DefaultComboBoxModel doctorModel;
	private DefaultComboBoxModel timeModel;
	private JComboBox comboBox_desiredDoctor;
	private JComboBox comboBox_time;
	private JTextArea reasonArea;
	private int year, month, day, hour, minute;	
	private Presenter presenter;
	private DefaultListModel appointmentModel;
	private String id;
	private JTextField txtField_scheduledTime;

	/**
	 * Create the frame.
	 * @param p presenter
	 * @param app appointment to view and edit
	 * @param m appointmentlist model to update
	 */
	public ViewEditAppointmentFrame(Presenter p, Appointment app, DefaultListModel m)
	{
		currentApp = app;
		presenter = p;
		appointmentModel = m;
		id = app.getId();
		
		setTitle("View/Edit Appointment"); // $codepro.audit.disable stringLiterals
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 332, 456); // $codepro.audit.disable numericLiterals
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); // $codepro.audit.disable numericLiterals
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		Calendar calendar = Calendar.getInstance();
		year = calendar.get(Calendar.YEAR);
		month = calendar.get(Calendar.MONTH) + 1;		
		day = calendar.get(Calendar.DAY_OF_MONTH);
		hour = calendar.get(Calendar.HOUR_OF_DAY);
		minute = calendar.get(Calendar.MINUTE);
		
		doctorModel = new DefaultComboBoxModel();
		for (Doctor e : presenter.getDoctorList())
		{
			doctorModel.addElement(e.getFullName());
		}
		
		timeModel = new DefaultComboBoxModel();
		DailySchedule selectedDay = presenter.getDailySchedule(app.getDate());
		
		Appointment[] appointments = selectedDay.getAppointments();

		for (int i = 0; i < appointments.length; i++)
		{
			int timeHour = (i * 30 + 540) / 60; // $codepro.audit.disable numericLiterals, variableDeclaredInLoop
			int timeMinute = (i * 30 + 540) % 60; // $codepro.audit.disable numericLiterals, variableDeclaredInLoop

			if (null == appointments[i] && (selectedDay.getDay() != day || selectedDay.getMonth() != month || selectedDay.getYear() != year
					|| timeHour > hour || (timeHour == hour && timeMinute > minute )))
			{
				timeModel.addElement((new Time(timeHour, timeMinute)));
			}
		}		
		
		JLabel lblDesiredDoctor = new JLabel("Desired Doctor:"); // $codepro.audit.disable stringLiterals
		lblDesiredDoctor.setBounds(24, 28, 82, 14); // $codepro.audit.disable numericLiterals
		contentPane.add(lblDesiredDoctor);
		
		JLabel lblDate = new JLabel("Date (YYYY/MM/DD):"); // $codepro.audit.disable stringLiterals
		lblDate.setBounds(24, 63, 116, 22); // $codepro.audit.disable numericLiterals
		contentPane.add(lblDate);
		
		JLabel lblchangeTime = new JLabel("Change Time:"); // $codepro.audit.disable stringLiterals
		lblchangeTime.setBounds(24, 139, 82, 14); // $codepro.audit.disable numericLiterals
		contentPane.add(lblchangeTime);
		
		comboBox_desiredDoctor = new JComboBox(doctorModel);
		comboBox_desiredDoctor.setSelectedItem(app.getDesiredDoctor());
		comboBox_desiredDoctor.setBounds(137, 25, 155, 20); // $codepro.audit.disable numericLiterals
		contentPane.add(comboBox_desiredDoctor);
		
		comboBox_time = new JComboBox(timeModel);
		comboBox_time.setSelectedItem(app.getTime());
		comboBox_time.setBounds(137, 136, 107, 20); // $codepro.audit.disable numericLiterals
		comboBox_time.setSelectedIndex(-1);
		contentPane.add(comboBox_time);
		
		JLabel lblReason = new JLabel("Reason:"); // $codepro.audit.disable stringLiterals
		lblReason.setBounds(24, 189, 46, 14); // $codepro.audit.disable numericLiterals
		contentPane.add(lblReason);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(24, 214, 269, 134); // $codepro.audit.disable numericLiterals
		contentPane.add(scrollPane);
		
		reasonArea = new JTextArea();
		reasonArea.setText(app.getReason());
		scrollPane.setViewportView(reasonArea);
		
		JButton btnBack = new JButton("Back"); // $codepro.audit.disable stringLiterals
		btnBack.setBounds(176, 371, 116, 23);
		contentPane.add(btnBack);
		
		JButton btnUpdate = new JButton("Update"); // $codepro.audit.disable stringLiterals
		btnUpdate.setBounds(24, 371, 126, 23);
		contentPane.add(btnUpdate);
		
		txtField_date = new JTextField();
		txtField_date.setEditable(false);
		txtField_date.setEnabled(false);
		txtField_date.setBounds(137, 64, 155, 20);
		contentPane.add(txtField_date);
		txtField_date.setColumns(10);
		txtField_date.setText(app.getDate().toString());
		
		txtField_scheduledTime = new JTextField();
		txtField_scheduledTime.setEnabled(false);
		txtField_scheduledTime.setEditable(false);
		txtField_scheduledTime.setBounds(137, 99, 107, 20);
		contentPane.add(txtField_scheduledTime);
		txtField_scheduledTime.setColumns(10);
		txtField_scheduledTime.setText(app.getTime().toString());
		
		JLabel lblScheduledTime = new JLabel("Scheduled Time:"); // $codepro.audit.disable stringLiterals
		lblScheduledTime.setBounds(24, 102, 107, 14);
		contentPane.add(lblScheduledTime);
		
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				Appointment newApp;
				
				if (comboBox_time.getSelectedIndex() == -1) //Changing only doctor
				{
					newApp = new Appointment(currentApp.getId(), currentApp.getDate(), currentApp.getTime(), 
							(String) comboBox_desiredDoctor.getSelectedItem(), reasonArea.getText());
				}
				else //Changing time as well
				{
					newApp = new Appointment(currentApp.getId(), currentApp.getDate(), (Time) comboBox_time.getSelectedItem(), 
						(String) comboBox_desiredDoctor.getSelectedItem(), reasonArea.getText());
				}
				boolean success = presenter.changeAppointment(currentApp, newApp);
				if (success)
				{
					JOptionPane.showMessageDialog(null, "Successfully updated appointment",  // $codepro.audit.disable stringLiterals
							"Error", JOptionPane.INFORMATION_MESSAGE); // $codepro.audit.disable stringLiterals
				}
				else
				{
					JOptionPane.showMessageDialog(null, "Update failed",  // $codepro.audit.disable stringLiterals
							"Error", JOptionPane.INFORMATION_MESSAGE); // $codepro.audit.disable stringLiterals
				}
				
				appointmentModel.clear();

				for (Appointment app : ((Patient) presenter.getUser(id)).getAppointments())
				{
					appointmentModel.addElement(app);
				}
				
				setVisible(false);
				dispose();
			}
		});
		
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				setVisible(false);
				dispose();
			}
		});
	}	
}
